package com.ciaojian.store.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.mapper.StorePerformanceMapper;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.StorePerformance;
import com.ciaojian.core.model.dto.FeesetCostDTO;
import com.ciaojian.core.model.dto.MemberDetailDTO;
import com.ciaojian.core.service.StorePerformanceService;
import com.ciaojian.store.model.query.DashboardQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author Atlakyn
 */
@Service
public class StorePerformanceServiceImpl extends StorePerformanceService<StorePerformanceMapper, StorePerformance> {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderServiceImpl orderService;
    /**
     * 剪发订单
     */
    private final List<Integer> CUT_HAIR_TYPE = new ArrayList<>(Arrays.asList(1, 9));

    /**
     * 员工业绩
     *
     * @param page  分页插件
     * @param query 参数
     * @return /
     */
    public Page<Map<String, Object>> workerPerformance(Page<Map<String, BigDecimal>> page,
                                                       DashboardQuery query) {
        MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
        BeanUtil.copyProperties(query, memberDetailDTO);
        return orderMapper.workerPerformance(page, memberDetailDTO, query.getOrderBy());
    }

    /**
     * 员工会员业绩
     *
     * @param page  分页插件
     * @param query 参数
     * @return /
     */
    public Page<Map<String, Object>> workerFeesetcost(Page<Map<String, BigDecimal>> page,
                                                       DashboardQuery query) {
        FeesetCostDTO feesetCostDTO = new FeesetCostDTO();
        BeanUtil.copyProperties(query, feesetCostDTO);
        return orderMapper.workerFeesetcost(page, feesetCostDTO, query.getOrderBy());
    }

    /**
     * 门店业绩排行
     *
     * @param query 查询参数
     * @return 业绩数据
     */
    public Page<Map<String, BigDecimal>> storeRank(DashboardQuery query, Page<Map<String, BigDecimal>> page) {
        MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
        BeanUtil.copyProperties(query, memberDetailDTO);
        return orderMapper.storeRank(page, memberDetailDTO);
    }

    /**
     * 订单数
     *
     * @param query      查询条件
     * @param orderTypes 订单类型
     * @return /
     */
    public Integer num(DashboardQuery query, List<Integer> orderTypes) {
        return orderService.lambdaQuery()
                .and(query.getWorkerId() != null, wrapper -> wrapper.and
                        (i -> i.eq(Order::getWorkerId, query.getWorkerId())
                                .or()
                                .eq(Order::getInviterId, query.getWorkerId())
                        ))
                .eq(query.getStoreId() != null, Order::getStoreId, query.getStoreId())
                .eq(query.getCustomerType() != null, Order::getCustomerType, query.getCustomerType())
                .between(Order::getAddTime, query.getStartDate(), query.getEndDate())
                .in(Order::getOrderType, orderTypes)
                .in(Order::getStatus, 3, 5)
                .like(StrUtil.isNotBlank(query.getType()), Order::getType, query.getType())
                .count();
    }

    /**
     * 订单数(根据会员字段)
     *
     * @param query      查询条件
     * @param orderTypes 订单类型
     * @return /
     */
    public Integer numIsMeber(DashboardQuery query) {
        return orderService.lambdaQuery()
                .eq(query.getStoreId() != null, Order::getStoreId, query.getStoreId())
                .in(Order::getIsUserMember, 0,1)
                .between(Order::getPayTime, query.getStartDate(), query.getEndDate())
                .in(Order::getOrderType, 11)
                .in(Order::getStatus, 3, 5)
                .count();
    }
    /**
     * 订单数(根据会员字段)
     *
     * @param query      查询条件
     * @param orderTypes 订单类型
     * @return /
     */
    public Integer numNotMeber(DashboardQuery query) {
        return orderService.lambdaQuery()
                .eq(query.getStoreId() != null, Order::getStoreId, query.getStoreId())
                .in(Order::getIsUserMember, -1)
                .between(Order::getPayTime, query.getStartDate(), query.getEndDate())
                .in(Order::getOrderType, 11)
                .in(Order::getStatus, 3, 5)
                .count();
    }


    /**
     * 订单数
     *
     * @param query 查询条件
     * @return /
     */
    public Integer num(DashboardQuery query) {
        return orderService.lambdaQuery()
                .and(query.getWorkerId() != null, wrapper -> wrapper.and
                        (i -> i.eq(Order::getWorkerId, query.getWorkerId())
                                .or()
                                .eq(Order::getInviterId, query.getWorkerId())
                        ))
                .eq(query.getStoreId() != null, Order::getStoreId, query.getStoreId())
                .eq(query.getCustomerType() != null, Order::getCustomerType, query.getCustomerType())
                .between(Order::getAddTime, query.getStartDate(), query.getEndDate())
                .in(Order::getOrderType, query.getOrderTypes())
                .in(Order::getStatus, 3, 5)
                .count();
    }

    /**
     * 销售总额 (含所有项目、会员)
     *
     * @param query 查询条件
     * @return /
     */
    public BigDecimal allSales(DashboardQuery query) {
        MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
        BeanUtil.copyProperties(query, memberDetailDTO);
        // 这里是19.9的会员销售
//        BigDecimal sales = memberOrderMapper.allSales(memberDetailDTO);
        // 这里是所有项目的销售额
        return orderMapper.allSales(memberDetailDTO);
    }

    /**
     * 销售总额 (抖音)
     *
     * @param query 查询条件
     * @return /
     */
    public BigDecimal dySales(DashboardQuery query) {
        MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
        BeanUtil.copyProperties(query, memberDetailDTO);
        // 这里是19.9的会员销售
//        BigDecimal sales = memberOrderMapper.allSales(memberDetailDTO);
        // 这里是所有项目的销售额
        return orderMapper.dySales(memberDetailDTO);
    }

    /**
     * 剪发销售额
     *
     * @param query /
     * @return /
     */
    public BigDecimal salesOfCutHair(DashboardQuery query) {
        MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
        BeanUtil.copyProperties(query, memberDetailDTO);
        return orderMapper.allSales(memberDetailDTO.setOrderTypes(CUT_HAIR_TYPE));
    }

    /**
     * 会员销售总额
     *
     * @param query 查询条件
     * @return /
     */
    public BigDecimal salesOfMember(DashboardQuery query) {
        MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
        BeanUtil.copyProperties(query, memberDetailDTO);
//        // 这里是19.9的会员销售
//        BigDecimal sales = memberOrderMapper.allSales(memberDetailDTO);
        // 这里是权益的销售
        return orderMapper.allSales(
                memberDetailDTO.setOrderTypes(Collections.singletonList(11)));
    }

    /**
     * 会员销售总额(按类型分组查询)
     *
     * @param query 查询条件
     * @return /
     */
    public List<Map<String, BigDecimal>> salesOfMemberGroup(DashboardQuery query) {
        MemberDetailDTO memberDetailDTO = new MemberDetailDTO();
        BeanUtil.copyProperties(query, memberDetailDTO);
        return orderMapper.sumMemberSalesGroup(memberDetailDTO);
    }

    /**
     * 抖音核销引流为会员数量
     * 既有抖音核销订单, 又有权益订单
     *
     * @return 数量
     */
    public Integer douyinTurnMember(DashboardQuery query) {
        return orderService.getBaseMapper().countDouyinTurnMember(
                null,
                query.getStoreId(),
                query.getStartDate(),
                query.getEndDate());
    }

    /**
     * 新人会员转化数
     * goods_total = 19.9 AND pay_total = 0 且 没有抖音审核订单
     *
     * @return 数量
     */
    public Integer newCustomerTurnMember(DashboardQuery query) {
        return orderService.getBaseMapper().countNewCustomerTurnMember(query.getStoreId(),
                query.getStartDate(), query.getEndDate());
    }
}
